package com.demo.excel;//

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName ExcelParamModel
 * @Description 定义导出Excel表格格式
 * @Author zqc
 * @Date 2020/5/28 16:13
 * @Version 1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExcelParamModel {
    private String fileName; // 导出文件名称
    private String sheetName; //sheet页名称
    private String titleName; //文件头名称
    private int columnNumber; //列数
    private int[] columnWidth; //每一个列宽度
    private String[] columnName; //表头
    private String[][] dataList; //数据
    private HttpServletResponse response; //返回
    private String filter; //数据筛选框
    private String subTitle; // 自定义导出副标题


    /**
     * 构造导出excel的参数
     *
     * @param fileName    导出的文件名称、she页名称、导出的excel标题 三合一
     * @param columnName  表头字段
     * @param columnWidth 每一列的宽度
     * @param data        数据
     * @param use         要使用的字段名称
     * @param customField (此参数是画龙点睛之笔)需要特殊处理的字段,如时间日期转换
     * @return
     */
    public static ExcelParamModel of(String fileName, String[] columnName, int[] columnWidth,
                                     List data, String[] use, CustomField customField,
                                     String filter, String subTitle) {
        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        String[][] dataList = ListToArray.listToArrayLambda(data, use, customField);
        return ExcelParamModel.builder()
                .fileName(fileName)
                .sheetName(fileName)
                .titleName(fileName)
                .columnNumber(columnName.length)
                .columnWidth(columnWidth)
                .columnName(columnName)
                .dataList(dataList)
                .response(attributes.getResponse())
                .filter(filter)
                .subTitle(subTitle)
                .build();
    }

    public static ExcelParamModel of(String fileName, String[] columnName,
                                     int[] columnWidth, List data, String[] use) {
        return of(fileName, columnName, columnWidth, data, use, null, null, null);
    }


    public static ExcelParamModel of(String fileName, String[] columnName, int[] columnWidth,
                                     List data, String[] use, CustomField customField) {
        return of(fileName, columnName, columnWidth, data, use, customField, null, null);
    }

    public static ExcelParamModel of(String fileName, String[] columnName, int[] columnWidth,
                                     List data, String[] use, CustomField customField, String filter) {
        return of(fileName, columnName, columnWidth, data, use, customField, filter, null);
    }

}
